//
//  textSSViewController.m
//  XiangMuB
//
//  Created by lanou3g on 16/8/23.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "textSSViewController.h"
//导入头文件
//科大讯飞语音识别功能回调方法的接口文件(协议)
#import <iflyMSC/IFlyRecognizerViewDelegate.h>

//科大讯飞语音识别功能的声音识别视图
#import <iflyMSC/IFlyRecognizerView.h>

//科大讯飞语音识别功能:自定义常量
#import <iflyMSC/IFlySpeechConstant.h>

#import "loadShare.h"
#import "NSObject+alertView.h"

@interface textSSViewController ()<IFlyRecognizerViewDelegate>//遵循协议
@property (strong,nonatomic)UITextView *showtextView;
//语音识别视图
@property (strong,nonatomic)IFlyRecognizerView *iflyRecognizerView;
//接收相关结果的数组
@property (strong,nonatomic)NSMutableString *resultString;


@property (strong,nonatomic)UITextField *textFired;//收件人姓名
@end

@implementation textSSViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //背景图片
    UIImageView *image =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"xin.jpg"]];
    [self.view addSubview:image];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    self.showtextView =[[UITextView alloc] initWithFrame:CGRectMake(10, 200, self.view.frame.size.width-20, 300)];
    
    self.showtextView.font = [UIFont systemFontOfSize:22];
    
    
    self.showtextView.backgroundColor = [UIColor colorWithRed:192.0f/255.0f  green:192.0f/255.0f  blue:192.0f/255.0f  alpha:0.5];
    
    self.showtextView.textColor = [UIColor orangeColor];
    self.showtextView.font = [UIFont systemFontOfSize:17];
    [self.view addSubview:self.showtextView];
    
    
    //发送给
    self.view.backgroundColor =[UIColor whiteColor];
    
    UILabel *lable  =[[UILabel alloc] initWithFrame:CGRectMake(20, 175, 80, 20)];
    lable.text = @"发送给:";
    lable.backgroundColor = [UIColor clearColor];
    [self.view addSubview:lable];
    
    //收件人
    _textFired =[[UITextField alloc] initWithFrame:CGRectMake(100, 175, 100, 20)];
    _textFired.backgroundColor = [UIColor colorWithRed:192.0f/255.0f  green:192.0f/255.0f  blue:192.0f/255.0f  alpha:0.5];
    
    _textFired.placeholder = @"收件人姓名";
    [self.view addSubview:_textFired];
    
    
    //发送
    UIButton *button1 =[UIButton buttonWithType:(UIButtonTypeCustom)];
    button1.backgroundColor = [UIColor orangeColor];
    button1.frame =CGRectMake(350, 175, 50, 20);
    [button1 addTarget:self action:@selector(BtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [button1 setTitle:@"发送" forState:(UIControlStateNormal)];
     [self.view addSubview:button1];
    

    
    //名人名言
    self.view.backgroundColor =[UIColor whiteColor];
    
    UILabel *lable1  =[[UILabel alloc] initWithFrame:CGRectMake(20, 510, self.view.frame.size.width -40 , 100)];
    lable1.text = @"与人交谈一次，往往比多年闭门劳作更能启发心智。思想必定是在与人交往中产生，而在孤独中进行加工和表达.";
    lable1.backgroundColor = [UIColor clearColor];
    lable1.numberOfLines = 0;
    [self.view addSubview:lable1];
    
    UILabel *lable2  =[[UILabel alloc] initWithFrame:CGRectMake(20, 580, self.view.frame.size.width -40 ,50)];
    lable2.text = @" ——列夫·托尔斯泰";
    
    lable2.textAlignment =  NSTextAlignmentRight;
    lable2.backgroundColor = [UIColor clearColor];
    lable2.numberOfLines = 0;
    [self.view addSubview:lable2];
    
    //语音转文字
    UIButton *button =[UIButton buttonWithType:(UIButtonTypeCustom)];
    button.backgroundColor = [UIColor clearColor];
    button.frame =CGRectMake(180, 480, 50, 50);
    [button addTarget:self action:@selector(Btn:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [button setTag:1000];
    [button setImage:[UIImage imageNamed:@"feng.png"] forState:(UIControlStateNormal)];
    
    [self.view addSubview:button];
    
    
    //初始化声音识别视图对象
    self.iflyRecognizerView = [[IFlyRecognizerView alloc]initWithCenter:self.view.center];
    
    self.iflyRecognizerView.delegate = self;
    
    
    
    
    
    
    
    
    
    
    
}


//发送点击事件
-(void)BtnAction:(UIButton *)sender
{
    
    
    //存收件人信息
    
    
    if ([_textFired.text isEqualToString:@""]||[_showtextView.text isEqualToString:@""]) {
        
        [NSObject alterString:@"消息或收件人不能为空"];
    }else
    {
    
        
        
           
    }
    
 
    
    
}

//语音识别点击事件
-(void)Btn:(UIButton *)sender
{
    [self startListenning];
    
    
    
}
-(void)startListenning
{
    //设置语音识别结果应用于普通文本领域
    [self.iflyRecognizerView setParameter:@"iat" forKey:[IFlySpeechConstant IFLY_DOMAIN]];
    
    //设置前端点检测时间为5000ms
    [self.iflyRecognizerView setParameter:@"5000" forKey:[IFlySpeechConstant VAD_BOS]];
    //设置后端点检测时间为700ms
    [self.iflyRecognizerView setParameter:@"700" forKey:[IFlySpeechConstant VAD_EOS]];
    //采样率为8000
    [self.iflyRecognizerView setParameter:@"8000" forKey:[IFlySpeechConstant SAMPLE_RATE]];
    //设置返回结果中包含的标点符号
    //[self.iflyRecognizerView setParameter:@"1" forKey:[IFlySpeechConstant ASR_PTT]];
    //设置语音识别完成之后数据的返回数据结构类型
    [self.iflyRecognizerView setParameter:@"plain" forKey:[IFlySpeechConstant RESULT_TYPE]];
    //设置在Document文件夹下缓存的文件名为"temp.asr"
    [self.iflyRecognizerView setParameter:@"temp.asr" forKey:[IFlySpeechConstant ASR_AUDIO_PATH]];
    
    
    //设置自定义参数
    [self.iflyRecognizerView setParameter:@"custom" forKey:[IFlySpeechConstant PARAMS]];
    //开始识别语音
    [self.iflyRecognizerView start];
    
}

-(void)onResult:(NSArray *)resultArray isLast:(BOOL)isLast{
    //初始化结果字符串
    self.resultString = [[NSMutableString alloc] init];
    NSDictionary *dic = [resultArray objectAtIndex:0];
    
    for ( NSString *str in dic) {
        
        [self.resultString appendFormat:@"%@",str];
    }
    
    //自定义控件的显示
    self.showtextView.text = [NSString stringWithFormat:@"%@,%@",self.showtextView.text,self.resultString];
    
}
-(void)onError:(IFlySpeechError *)error
{
    NSLog(@"失败");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
